<?php
/* /app/views/helpers/postal.php */
 
class PostalHelper extends AppHelper {

	function format($code, $country = 'US') {
		
		if($country == 'US') {
			$code = preg_replace("/[^0-9]/", "", $code);
			if(strlen($code) == 5)
			    return $code;
		    elseif(strlen($code) == 9)
			    return preg_replace("/([0-9]{5})([0-9]{4})/", "$1-$2", $code);
		    else
			    return $code;
			
		} elseif($country == 'CA') {
			$code = preg_replace("/[^a-zA-Z0-9]/", "", $code);
			if(strlen($code) == 6)
			    return preg_replace("/([a-zA-Z0-9]{3})([a-zA-Z0-9]{3})/", "$1 $2", $code);
		    else
			    return $code;
		}
	}
} 
?>